<template>
	<view>
		<!-- 显示bmi指数跟体测总分 -->
		<view class="top uni-flex">
			<view class="flex-item ">
				体质指数(BMI)
				<view class="">
					{{bmi_score}}
					<span v-if="this.bmi == 0">{{bmi}}</span>
					<span v-else>({{bmi}})</span>
				</view>
			</view>
			<view class="">|</view>
			<view class="flex-item">
				体测总成绩
				<view class="">
					{{sum_score}}
					({{sum}})
				</view>
			</view>
		</view>
		<!-- 各项值输入 -->
		<view class="bottom">
			<u-form :model="form" ref="uForm">
				<u-form-item label="性别" label-width="160rpx">
					<u-select v-model="show_sex" mode="single-column" :list="form.sex" @confirm="confirm_sex">
					</u-select>
					<u-input v-model="label_sex" :border="border" @click="show_sex = true" type="select" />
				</u-form-item>
				<u-form-item label="年级" label-width="160rpx">
					<u-select v-model="show_grade" mode="single-column" :list="form.grade" @confirm="confirm_grade">
					</u-select>
					<u-input v-model="label_grade" :border="border" @click="show_grade = true" type="select" />
				</u-form-item>
				<u-form-item label="身高" label-width="160rpx">
					<u-input v-model="form.height" :border="border" placeholder="输入身高" />
					<span>厘米</span>
				</u-form-item>
				<u-form-item label="体重" label-width="160rpx">
					<u-input v-model="form.weight" :border="border" placeholder="输入体重" />
					<span>千克---{{score.weight_score}}分</span>
				</u-form-item>
				<u-form-item label="肺活量" label-width="160rpx">
					<u-input v-model="form.vital_capacity" :border="border" placeholder="输入肺活量" />
					<span>毫升---{{score.vital_capacity_score}}分</span>
				</u-form-item>
				<u-form-item label="坐位体前屈" label-width="160rpx">
					<u-input v-model="form.sit_and_reach" :border="border" placeholder="输入长度" />
					<span>厘米---{{score.sit_and_reach_score}}分</span>
				</u-form-item>
				<u-form-item label="立定跳远" label-width="160rpx">
					<u-input v-model="form.standing_long_jump" :border="border" placeholder="输入长度" />
					<span>厘米---{{score.standing_long_jump_score}}分</span>
				</u-form-item>
				<u-form-item label="50米跑" label-width="160rpx">
					<u-input v-model="form.race_50m" :border="border" placeholder="输入时长" />
					<span>秒---{{score.race_50m_score}}分</span>
				</u-form-item>
				<u-form-item v-if="label_sex == '男'" label="1000米跑" label-width="160rpx">
					<u-input v-model="form.race_1000m" :border="border" placeholder="输入时长" />
					<span>秒---{{score.race_1000m_score}}分</span>
				</u-form-item>
				<u-form-item v-else-if="label_sex == '女'" label="800米跑" label-width="160rpx">
					<u-input v-model="form.race_1000m" :border="border" placeholder="输入时长" />
					<span>秒---{{score.race_800m_score}}分</span>
				</u-form-item>
				<u-form-item v-if="label_sex == '男'" label="引体向上" label-width="160rpx">
					<u-input v-model="form.pull_up" :border="border" placeholder="输入次数" />
					<span>次---{{score.pull_up_score}}分</span>
				</u-form-item>
				<u-form-item v-else-if="label_sex == '女'" label="仰卧起坐" label-width="160rpx">
					<u-input v-model="form.sit_up" :border="border" placeholder="输入次数" />
					<span>次---{{score.sit_up_score}}分</span>
				</u-form-item>
			</u-form>
		</view>
	</view>
</template>

<script>
	import scoreList from '../../server/standard/score.js'
	import gradeList from '../../server/standard/grade.js'
	export default {
		data() {
			return {
				score_list: scoreList, //数据对应分数
				grade_list: gradeList, //体质指数与总成绩对应评价
				bmi: '', //bmi评价
				sum: '', //体测总成绩评价
				show_sex: false, //下拉框显示
				show_grade: false,
				border: true,
				label_sex: '男',
				value_sex: 'male', //用于分数匹配
				label_grade: '大一',
				value_grade: 'freshman_sophomore', //用于分数匹配
				form: {
					sex: [{
							value: 'male',
							label: '男'
						},
						{
							value: 'female',
							label: '女'
						}
					],
					grade: [{
							value: 'freshman_sophomore',
							label: '大一'
						},
						{
							value: 'freshman_sophomore',
							label: '大二'
						},
						{
							value: 'junior_senior',
							label: '大三'
						},
						{
							value: 'junior_senior',
							label: '大四'
						}
					],
					weight: '', //体重, 千克
					height: '', //身高, 厘米
					vital_capacity: '', //肺活量, 毫升
					race_50m: '', //50米跑, 秒
					sit_and_reach: '', //坐立前屈, 厘米
					standing_long_jump: '', //立定跳远, 厘米
					race_1000m: '', //1000米跑(男), 秒	
					pull_up: '', //引体向上(男), 个
					race_800m: '', //800米跑(女), 秒
					sit_up: '' //仰卧起坐(女), 个
				},
				bmi_score: 0, //bmi体质指数
				score: {
					weight_score: 0,
					vital_capacity_score: 0,
					sit_and_reach_score: 0,
					standing_long_jump_score: 0,
					race_50m_score: 0,
					race_1000m_score: 0,
					pull_up_score: 0,
					race_800m_score: 0,
					sit_up_score: 0
				},
				score_percent: {
					weight_score: 0.15,
					vital_capacity_score: 0.15,
					sit_and_reach_score: 0.1,
					standing_long_jump_score: 0.1,
					race_50m_score: 0.2,
					race_1000m_score: 0.2,
					pull_up_score: 0.1,
					race_800m_score: 0.2,
					sit_up_score: 0.1
				}
			}
		},
		//监听页面加载
		onLoad() {
			//this.bmi_sum_grade()
		},
		updated() {

		},
		methods: {
			//下拉框回调参数
			confirm_sex(e) {
				this.label_sex = e[0].label
				this.value_sex = e[0].value
			},
			confirm_grade(e) {
				this.label_grade = e[0].label
				this.value_grade = e[0].value
			},
			//体质指数与总成绩对应评价
			bmi_sum_grade() {
				for (let i = 0; i < 2; i++) {
					if (Object.keys(gradeList.data)[i] == this.value_sex) {
						let grade = Object.values(gradeList.data)[i];
						for (let j = 0; j < 2; j++) {
							if (Object.keys(grade)[j] == 'aggregate') {
								let standard = Object.values(grade)[j];
								let values = standard[0],
									rules = standard[1];
								let len = rules.length;
								for (let i = 0; i < len; i++) {
									let rule = rules[i],
										value = values[i];
									let min = rule[0] === 'min' ? -Infinity : rule[0],
										max = rule[1] === 'max' ? Infinity : rule[1];
									if (this.sum_score >= min && this.sum_score <= max) {
										this.sum = value
									}
								}
							} else if (Object.keys(grade)[j] == 'bmi') {
								let standard = Object.values(grade)[j];
								let values = standard[0],
									rules = standard[1];
								let len = rules.length;
								for (let i = 0; i < len; i++) {
									let rule = rules[i],
										value = values[i];
									let min = rule[0] === 'min' ? -Infinity : rule[0],
										max = rule[1] === 'max' ? Infinity : rule[1];
									if (this.bmi_score >= min && this.bmi_score <= max) {
										this.bmi = value
									}
								}
							} else {
								this.sum = '';
								this.bmi = ''
							}
						}
					}
				}
			},
			//显示各个模块对应分数
			//bmi指数对应分数
			weight_score() {
				for (var i in this.score_list.data) {
					if (this.value_sex == i) {
						for (var j in this.score_list.data[i]) {
							if (this.value_grade == j) {
								for (var k in this.score_list.data[i][j]) {
									if ('bmi' == k) {
										let standard = this.score_list.data[i][j][k];
										let values = standard[0],
											rules = standard[1];
										let len = rules.length;
										for (let i = 0; i < len; i++) {
											let rule = rules[i],
												value = values[i];
											if (!Array.isArray(rule[0])) {
												let min = rule[0] === 'min' ? -Infinity : rule[0],
													max = rule[1] === 'max' ? Infinity : rule[1];
												if (this.bmi_score >= min && this.bmi_score <= max) {
													this.score.weight_score = value
												}
											} else {
												for (let j = 0; j < rule[0].length; j++) {
													let min = rule[j][0] === 'min' ? -Infinity : rule[j][0],
														max = rule[j][1] === 'max' ? Infinity : rule[j][1];
													if (this.bmi_score >= min && this.bmi_score <= max) {
														this.score.weight_score = value
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			},
			//肺活量对应分数
			vital_capacity_score() {
				for (var i in this.score_list.data) {
					if (this.value_sex == i) {
						for (var j in this.score_list.data[i]) {
							if (this.value_grade == j) {
								for (var k in this.score_list.data[i][j]) {
									if ('vital_capacity' == k) {
										let standard = this.score_list.data[i][j][k];
										let values = standard[0],
											rules = standard[1];
										let len = rules.length;
										for (let i = 0; i < len; i++) {
											let rule = rules[i],
												value = values[i],
												first = rules[0]
											if (this.form.vital_capacity < first) {
												this.score.vital_capacity_score = 0
											} else if (this.form.vital_capacity >= rule) {
												this.score.vital_capacity_score = value
											}
										}
									}
								}
							}
						}
					}
				}
			},
			//坐位体前屈对应分数
			sit_and_reach_score() {
				for (var i in this.score_list.data) {
					if (this.value_sex == i) {
						for (var j in this.score_list.data[i]) {
							if (this.value_grade == j) {
								for (var k in this.score_list.data[i][j]) {
									if ('sit_and_reach' == k) {
										let standard = this.score_list.data[i][j][k];
										let values = standard[0],
											rules = standard[1];
										let len = rules.length;
										for (let i = 0; i < len; i++) {
											let rule = rules[i],
												value = values[i]
											if (this.form.sit_and_reach.length == 0) {
												this.score.sit_and_reach_score = 0
											}
											if (this.form.sit_and_reach >= rule) {
												this.score.sit_and_reach_score = value
											}
										}
									}
								}
							}
						}
					}
				}
			},
			//立定跳远分数
			standing_long_jump_score() {
				for (var i in this.score_list.data) {
					if (this.value_sex == i) {
						for (var j in this.score_list.data[i]) {
							if (this.value_grade == j) {
								for (var k in this.score_list.data[i][j]) {
									if ('standing_long_jump' == k) {
										let standard = this.score_list.data[i][j][k];
										let values = standard[0],
											rules = standard[1];
										let len = rules.length;
										for (let i = 0; i < len; i++) {
											let rule = rules[i],
												value = values[i],
												first = rules[0]
											if (this.form.standing_long_jump < first) {
												this.score.standing_long_jump_score = 0
											} else if (this.form.standing_long_jump >= rule) {
												this.score.standing_long_jump_score = value
											}
										}
									}
								}
							}
						}
					}
				}
			},
			//50米跑分数
			race_50m_score() {
				for (var i in this.score_list.data) {
					if (this.value_sex == i) {
						for (var j in this.score_list.data[i]) {
							if (this.value_grade == j) {
								for (var k in this.score_list.data[i][j]) {
									if ('race_50m' == k) {
										let standard = this.score_list.data[i][j][k];
										let values = standard[0],
											rules = standard[1];
										let len = rules.length;
										for (let i = 0; i < len; i++) {
											let rule = rules[i],
												value = values[i],
												first = rules[0]
											if (this.form.race_50m > first) {
												this.score.race_50m_score = 0
											} else if (this.form.race_50m <= rule) {
												this.score.race_50m_score = value
											}
										}
									}
								}
							}
						}
					}
				}
			},
			//1000米跑分数
			race_1000m_score() {
				for (var i in this.score_list.data) {
					if (this.value_sex == i) {
						for (var j in this.score_list.data[i]) {
							if (this.value_grade == j) {
								for (var k in this.score_list.data[i][j]) {
									if ('race_1000m' == k) {
										let standard = this.score_list.data[i][j][k];
										let values = standard[0],
											rules = standard[1];
										let len = rules.length;
										for (let i = 0; i < len; i++) {
											let rule = rules[i],
												value = values[i],
												first = rules[0]
											if (this.form.race_1000m > first) {
												this.score.race_1000m_score = 0
											} else if (this.form.race_1000m <= rule) {
												this.score.race_1000m_score = value
											}
										}
									}
								}
							}
						}
					}
				}
			},
			//引体向上分数
			pull_up_score() {
				for (var i in this.score_list.data) {
					if (this.value_sex == i) {
						for (var j in this.score_list.data[i]) {
							if (this.value_grade == j) {
								for (var k in this.score_list.data[i][j]) {
									if ('pull_up' == k) {
										let standard = this.score_list.data[i][j][k];
										let values = standard[0],
											rules = standard[1];
										let len = rules.length;
										for (let i = 0; i < len; i++) {
											let rule = rules[i],
												value = values[i],
												first = rules[0]
											if (this.form.pull_up < first) {
												this.score.pull_up_score = 0
											} else if (this.form.pull_up >= rule) {
												this.score.pull_up_score = value
											}
										}
									}
								}
							}
						}
					}
				}
			},
			//800米跑分数
			race_800m_score() {
				for (var i in this.score_list.data) {
					if (this.value_sex == i) {
						for (var j in this.score_list.data[i]) {
							if (this.value_grade == j) {
								for (var k in this.score_list.data[i][j]) {
									if ('race_800m' == k) {
										let standard = this.score_list.data[i][j][k];
										let values = standard[0],
											rules = standard[1];
										let len = rules.length;
										for (let i = 0; i < len; i++) {
											let rule = rules[i],
												value = values[i],
												first = rules[0],
												last = rules[len - 1]
											if (this.form.race_800m > first || this.form.race_800m < last) {
												this.score.race_800m_score = 0
											} else if (this.form.race_800m <= rule) {
												this.score.race_800m_score = value
											}
										}
									}
								}
							}
						}
					}
				}
			},
			//仰卧起坐分数
			sit_up_score() {
				for (var i in this.score_list.data) {
					if (this.value_sex == i) {
						for (var j in this.score_list.data[i]) {
							if (this.value_grade == j) {
								for (var k in this.score_list.data[i][j]) {
									if ('sit_up' == k) {
										let standard = this.score_list.data[i][j][k];
										let values = standard[0],
											rules = standard[1];
										let len = rules.length;
										for (let i = 0; i < len; i++) {
											let rule = rules[i],
												value = values[i],
												first = rules[0]
											if (this.form.sit_up < first) {
												this.score.sit_up_score = 0
											} else if (this.form.sit_up >= rule) {
												this.score.sit_up_score = value
											}
										}
									}
								}
							}
						}
					}
				}

			}
		},
		watch: {
			//监测计算bmi健康指数
			'form.height'() {
				let height = this.form.height / 100
				let weight = this.form.weight
				this.bmi_score = parseFloat(weight / (height ** 2)).toFixed(1)
				return this.bmi_score //bmi指数
			},
			'form.weight'() {
				let height = this.form.height / 100
				let weight = this.form.weight
				this.bmi_score = parseFloat(weight / (height ** 2)).toFixed(1)
				return this.bmi_score //bmi指数
			},
			//监听bmi指数
			bmi_score() {
				this.weight_score() //bmi指数对应分数
				this.bmi_sum_grade() //bmi指数对应评价
			},
			//监听肺活量
			'form.vital_capacity'() {
				this.vital_capacity_score()
			},
			//监听坐位体前屈
			'form.sit_and_reach'() {
				this.sit_and_reach_score()
			},
			//监听立定跳远
			'form.standing_long_jump'() {
				this.standing_long_jump_score()
			},
			//监听50米跑
			'form.race_50m'() {
				this.race_50m_score()
			},
			//监听1000米跑
			'form.race_1000m'() {
				this.race_1000m_score()
			},
			//监听引体向上
			'form.pull_up'() {
				this.pull_up_score()
			},
			//监听800米跑
			'form.race_800m'() {
				this.race_800m_score()
			},
			//监听仰卧起坐
			'form.sit_up'() {
				this.sit_up_score()
			},
			//监听性别变化
			label_sex() {
				this.vital_capacity_score(),
					this.sit_and_reach_score(),
					this.standing_long_jump_score(),
					this.race_50m_score(),
					this.race_1000m_score(),
					this.pull_up_score(),
					this.race_800m_score(),
					this.sit_up_score()
			},
			//监听年级变化
			label_grade() {
				this.vital_capacity_score(),
					this.sit_and_reach_score(),
					this.standing_long_jump_score(),
					this.race_50m_score(),
					this.race_1000m_score(),
					this.pull_up_score(),
					this.race_800m_score(),
					this.sit_up_score()
			},
			//监听体测总成绩
			sum_score() {
				this.bmi_sum_grade()
			}
		},
		computed: {
			//计算体测总成绩
			sum_score: function() {
				let sum = 0;
				for (var i in this.score) {
					for (var j in this.score_percent) {
						if (i == j) {
							sum += this.score[i] * this.score_percent[j]
						}
					}
				}
				return sum
			}
		}
	}
</script>

<style lang="scss">
	.u-form-item {
		line-height: 23px;

		span {
			position: relative;
			top: 0;
			right: 90px;
			display: table-cell;
			white-space: nowrap;
			padding: 7px 10px;
		}
	}

	.top {
		padding: 0;
		height: 100rpx;
	}

	.uni-flex {
		display: flex;
		flex-direction: row;

		.flex-item {
			height: 100%;
			width: 50%;
			text-align: center;
		}
	}

	.bottom {}
</style>
